home *** CD-ROM | disk | FTP | other *** search
/ Delphi Programmer's Power Pack / Delphi Volume 1.iso / e_to_l / fbuilder / c / fbcalc.h
Encoding:
C/C++ Source or Header  |  1996-09-15  |  13.9 KB  |  428 lines

  1. /*
  2.  *
  3.  *  C/C++ Header File for FORMULA BUILDER Version 1.0
  4.  *  An Advanced Expression Parsing/Evaluation Engine
  5.  *  YGB Software, Inc.
  6.  *  Copyright (c) 1995,  Clayton Collie, All Rights Reserved
  7.  *
  8.  */
  9.  
  10. #ifndef __FB_H__
  11. #define __FB_H__
  12.  
  13. #ifndef _INC_WINDOWS
  14. #include "windows.h"
  15. #endif
  16.  
  17.  
  18.  
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif  // __cplusplus
  22.  
  23. #define FB_VERSION  0x0100
  24.  
  25.  
  26. typedef LONG     HEXPR;    /*  expression handle type */
  27. typedef int      FBERROR;  /*  Formula Builder Error Type */
  28.  
  29. #define CALLBACK FAR PASCAL _export
  30. #define FBAPI    FAR PASCAL _export
  31.  
  32.  
  33. #define MAXFUNCPARAMS 16  /* Functions can have up to MAXFUNCPARAMS parameters */
  34.  
  35.  
  36. /*
  37.  *
  38.  *  Definitions of types handled by Formula Builder
  39.  *
  40.  */
  41. typedef BYTE   datatypes;
  42. typedef BYTE   BOOLEAN;
  43. typedef double FLOAT;   /* FormulaBuilder floating point type */
  44.  
  45. /*
  46.  * Dates are stored as a floating point value whose integer portion represents
  47.  * the number days elapsed since 01/01/001. The time of day, when time functions
  48.  * are implemented, will be represented by the fractional part.
  49.  *
  50.  */
  51.  
  52. typedef double TFBDate, TFBDATE, FAR *LPFBDATE;
  53.  
  54. /*
  55.  * Declaration of the internal FB string type - a pointer to a byte-string,
  56.  * a string in which the first byte designates the length, followed by the
  57.  * string data (which is limited to 255 characters). Use the utility routines
  58.  * to deal with this type
  59.  *
  60.  */
  61. typedef char FBString[256];
  62. typedef FBString *TFBSTRING, FAR *LPFBSTRING;
  63.  
  64.  
  65. /* NOTE!!!! the following are NOT contiguous */
  66.  
  67. #define vtINTEGER   0
  68. #define vtBOOLEAN   1
  69. #define vtCHAR      2     /* unused */
  70. #define vtFLOAT     3
  71. #define vtSTRING    4
  72. #define vtPOINTER   5
  73. #define vtDATE      9
  74. #define vtBOOL      10
  75. #define vtANY       11
  76. #define vtNONE      13
  77. #define vtTYPECLASH     14
  78. #define vtTYPEMISMATCH  vtTYPECLASH
  79.  
  80.  
  81. /*  General value structure */
  82.  
  83. typedef struct tagTValueRec {
  84.      BYTE      flags;
  85.      BYTE      vtype;
  86.      union {
  87.         LONG         vInteger;
  88.         BOOLEAN      vBoolean;
  89.         FLOAT        vFloat;    /* double */
  90.         TFBSTRING    vpString;
  91.         LPVOID       vPointer;
  92.         TFBDATE      vDate;
  93.         BOOL         vBool;
  94.     }
  95.  } TVALUEREC,TValueRec,*PVALUEREC,FAR *LPVALUEREC;
  96.  
  97.  
  98.  
  99. /*
  100.  *
  101.  *  Error Codes
  102.  *
  103.  */
  104.  
  105. #define INVALID_VARIABLE  -9999 /* Arbitrary but (hopefully) unique - return this from */
  106.  
  107. // Defines for Error Codes
  108.  
  109. #define EXPR_SUCCESS                      1
  110. #define EXPR_MISSING_PAREN                2
  111. #define EXPR_BAD_EXPRESSION               3
  112. #define EXPR_BAD_ASSIGNMENT               4
  113. #define EXPR_UNKNOWN_IDENT                5
  114. #define EXPR_LINE_TOO_LONG                6
  115. #define EXPR_INVALID_TOKEN                7
  116. #define EXPR_INVALID_CHAR                 8
  117. #define EXPR_MISSING_PARAM                9
  118. #define EXPR_TYPE_MISMATCH                10
  119. #define EXPR_INVALID_NUMBER               11
  120. #define EXPR_MISSING_VARIABLE             12
  121. #define EXPR_INVALID_VARIABLE             12
  122. #define EXPR_INVALID_FUNCTION             13
  123. #define EXPR_ZERO_DIVISION                14
  124. #define EXPR_STACK_OVERFLOW               15
  125. #define EXPR_UNEXPECTED_EOS               16
  126. #define EXPR_INVALID_DATE                 17
  127. #define EXPR_IDENTIFIER_EXPECTED          18
  128. #define EXPR_RANGE_ERROR                  19
  129. #define EXPR_DOMAIN_ERROR                 20
  130. #define EXPR_MATH_ERROR                   21
  131. #define EXPR_FP_OVERFLOW                  22
  132. #define EXPR_FP_UNDERFLOW                 23
  133. #define EXPR_INT_OVERFLOW                 24
  134. #define EXPR_INVALID_OP                   25
  135. #define EXPR_VARIABLE_EXPECTED            26
  136. #define EXPR_MISSING_OPERATOR             27
  137. #define EXPR_MISSING_OPERAND              28
  138. #define EXPR_CONSTANT_EXPECTED            29
  139. #define EXPR_DUPLICATE_IDENT              30
  140. #define EXPR_SYNTAX_ERROR                 31
  141. #define EXPR_CONVERT_ERROR                32
  142. #define EXPR_INVALID_TYPE                 33
  143. #define EXPR_INVALID_HANDLE               50
  144. #define EXPR_INVALID_CALLBACK             51
  145. #define EXPR_FORMULA_TOO_COMPLEX          54
  146.  
  147. #define EXPR_INVALID_VARIABLE  EXPR_MISSING_VARIABLE
  148.  
  149.  
  150. /*
  151.  *
  152.  * Callback definitions for variable & field implementation
  153.  *
  154.  */
  155.  
  156. typedef FBERROR (CALLBACK *TCBKFindVariable)(LPSTR varname,LPBYTE vtype,LONG vardata,LONG CBKData);
  157.  
  158. typedef FBERROR (CALLBACK *TCBKGetVariable)(LPSTR varname,LPVALUEREC value,LONG vardata,LONG CBKData);
  159.  
  160. typedef FBERROR (CALLBACK *TCBKSetVariable)(LPSTR varname,TVALUEREC value,LONG vardata,LONG CBKData);
  161.  
  162. /*
  163.  * function enumeration Callback. See FBEnumFunctions
  164.  */
  165.  
  166. typedef FBERROR (CALLBACK *TCBKEnumFunctions)(LPSTR vname,BYTE vtype,LPSTR parms,BYTE minPrms,LONG EnumData);
  167.  
  168. /******************************************************
  169.  *
  170.  *  Declarations for external function implementation
  171.  *
  172.  ******************************************************/
  173.  
  174.  
  175. /*
  176.  * Actual parameter list passed to external function callback
  177.  * The parser engine ensures that these match, in number and
  178.  * type, the prototype specified when the callback was registered
  179.  */
  180.  
  181.  typedef TVALUEREC TActParamList[MAXFUNCPARAMS];
  182.  typedef TActParamList *PActParamList,FAR *LPPARAMLIST;
  183.  
  184.  
  185. /*::::::::::::::::::::::::::::::::::::::::::::::::::::
  186.   Prototypes for external user-defined functions
  187.   Implemented routine must be exportable
  188.  ::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  189.  
  190.  typedef void (CALLBACK *TCBKExternalFunc)(BYTE          paramcount, \
  191.                                            LPPARAMLIST   params, \
  192.                                            LPVALUEREC    retvalue, \
  193.                                            LPINT         errcode, \
  194.                                            LONG          lExprData);
  195.      
  196.  
  197. /*:::::::::::::::Engine initialization and shutdown:::::::::::::*/
  198.  
  199.   HEXPR   FBAPI FBInitExpression(LONG lExprData);
  200.   FBERROR FBAPI FBFreeExpression(HEXPR handle);
  201.  
  202. /*::::::::::::::::::::: Expression Manipulation ::::::::::::::::*/
  203.  
  204.   FBERROR FBAPI  FBSetExpression(HEXPR handle,LPSTR expr);
  205.  
  206.   FBERROR FBAPI  FBReparseExpression(HEXPR handle);
  207.  
  208.   FBERROR FBAPI  FBClearExpression(HEXPR handle);
  209.  
  210.   FBERROR FBAPI  FBGetExpression(HEXPR handle,LPSTR expr,WORD maxlen);
  211.  
  212.  
  213. /*==========================================================================
  214.  *
  215.  *      EXPRESSION EVALUATION ROUTINES
  216.  *
  217.  *=========================================================================*/
  218.  
  219.  
  220.   /* determine the result type of the expression. Returns one of the
  221.      vtXXX constants, vtTYPEMISMATCH for an invalid expression      */
  222.  
  223.   FBERROR FBAPI FBGetReturnType(HEXPR handle);
  224.  
  225. /*
  226.    Evaluate the expression, returning a maximum of maxlen characters
  227.    of the null-terminated string result in the buffer/string pointed to
  228.    by outbuf                                                            */
  229.  
  230.  
  231.   FBERROR FBAPI  FBEvaluate(HEXPR handle,LPSTR lpszBuf,WORD wBuflen);
  232.  
  233.  /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  234.   Evaluate the expression, returning the result in a TValueRec structure
  235.   FBFreevalue should be used to dispose of any memory associated with value
  236.   when it is no longer needed
  237.   :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  238.  
  239.   FBERROR FBAPI  FBEvaluatePrim(HEXPR handle,LPVALUEREC value);
  240.  
  241.  
  242.  /* Dispose of any memory associated with a TValueRec structure          */
  243.  
  244.   void FBAPI FBFreeValue(LPVALUEREC value);
  245.  
  246.  /*
  247.   *
  248.   *
  249.   *
  250.   */
  251.  
  252.   FBERROR FBAPI FBGetStringResult(HEXPR handle,LPSTR value,WORD maxlen);
  253.   FBERROR FBAPI FBGetFloatResult(HEXPR handle,LPDOUBLE value);
  254.   FBERROR FBAPI FBGetBooleanResult(HEXPR handle,LPBOOL value);
  255.   FBERROR FBAPI FBGetIntResult(HEXPR handle,LPLONG value);
  256.   FBERROR FBAPI FBGetDateResult(HEXPR handle,LPFBDATE value);
  257.  
  258.  /*
  259.   Perform a single operation expression evaluation. This is not the most
  260.   efficient method of evaluation when the expression remains the same   */
  261.  
  262.   FBERROR FBAPI FBEvalExpression(LPSTR expr,LPBYTE retType,LPSTR buf,WORD maxlen);
  263.  
  264. /*::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  265.   Internal Variable handling routines. NOTE ! If the variable callbacks are
  266.   implemented, the evaluation engine will not see the variables added
  267.   by FBAddvariable
  268.   :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
  269.  
  270.  /*  Add a variable of type Vtype (see vtXXX constants) to the Expression Engine */
  271.  
  272.   FBERROR FBAPI  FBAddVariable(HEXPR handle,LPSTR vname,BYTE vtype);
  273.   FBERROR FBAPI  FBParseAddVariable(HEXPR handle,LPSTR vname,LPSTR expr);
  274.  
  275.  /*
  276.   * Set the value of variable vname from the string value
  277.   *
  278.   */
  279.  
  280.   FBERROR FBAPI  FBSetVarFromString(HEXPR handle,LPSTR vname,LPSTR value);
  281.   FBERROR FBAPI  FBSetVariablePrim(HEXPR handle,LPSTR vname,TVALUEREC value);
  282.  
  283.   FBERROR FBAPI FBSetStringVariable(HEXPR handle,LPSTR vname,LPCSTR value);
  284.   FBERROR FBAPI FBSetIntegerVariable(HEXPR handle,LPSTR vname,LONG value);
  285.   FBERROR FBAPI FBSetFloatVariable(HEXPR handle,LPSTR vname,DOUBLE value);
  286.   FBERROR FBAPI FBSetBooleanVariable(HEXPR handle,LPSTR vname,BOOL value);
  287.   FBERROR FBAPI FBSetDateVariable(HEXPR handle,LPSTR vname,TFBDATE value);
  288.  
  289.  
  290.  /*
  291.   * Retrieve the string representation of the value of variable vname
  292.   * into the null terminated string value.
  293.   */
  294.  
  295.   FBERROR FBAPI FBGetVarAsString(HEXPR handle,LPSTR vname,LPSTR value,WORD maxlen);
  296.  
  297.   FBERROR FBAPI FBGetStringVariable(HEXPR handle,LPSTR vname,LPSTR value,WORD maxlen);
  298.   FBERROR FBAPI FBGetIntegerVariable(HEXPR handle,LPSTR vname,LPLONG value);
  299.   FBERROR FBAPI FBGetFloatVariable(HEXPR handle,LPSTR vname,LPDOUBLE value);
  300.   FBERROR FBAPI FBGetBooleanVariable(HEXPR handle,LPSTR vname,LPBOOL value);
  301.   FBERROR FBAPI FBGetDateVariable(HEXPR handle,LPSTR vname,LPFBDATE value);
  302.  
  303.  /*
  304.   * Get a pointer to the data of a variable handled internally by FB
  305.   *
  306.   */
  307.  
  308.   FBERROR FBAPI FBGetVarPtr(HEXPR handle,LPSTR vname,LPBYTE vtype,LPVOID *value);
  309.  
  310.   FBERROR FBAPI FBPeekVariable(HEXPR handle,int vno,LPSTR vname,WORD maxlen,LPVALUEREC value);
  311.  
  312.   FBERROR FBAPI FBPeekVarVB(HEXPR handle,int vno,LPSTR vname,WORD maxnamelen,LPINT vtype,LPSTR value,WORD maxvallen);
  313.  
  314.   FBERROR FBAPI  FBFreeVariable(HExpr handle,LPSTR vname);
  315.  
  316.   FBERROR FBAPI  FBFreeVariableList(HEXPR handle);
  317.  
  318.   FBERROR FBAPI  FBGetVariableCount(HEXPR handle);
  319.  
  320.   /*  Error handling */
  321.  
  322.   void FBAPI FBGetErrorString(int iEcode,LPSTR lpszBuf,WORD iBufsz);
  323.  
  324.   FBERROR FBAPI FBGetErrorPos(HEXPR handle,LPINT errpos);
  325.  
  326. /*==========================================================================
  327.  *
  328.  *       CONSTANT HANDLING ROUTINES
  329.  *
  330.  *=========================================================================*/
  331.  
  332.  FBERROR FBAPI FBAddConstantPrim(LPSTR cname,LPVALUEREC value);
  333.  FBERROR FBAPI FBParseAddConst(LPSTR vname,LPSTR expr);
  334.  FBERROR FBAPI FBAddStringConstant(LPSTR cname,LPSTR value);
  335.  FBERROR FBAPI FBAddDateConstant(LPSTR cname,TFBDate value);
  336.  FBERROR FBAPI FBAddNumericConstant(LPSTR cname,double value);
  337.  FBERROR FBAPI FBAddBooleanConstant(LPSTR cname,BOOL value);
  338.  FBERROR FBAPI FBGetConstantPrim(LPSTR cname,LPVALUEREC value);
  339.  FBERROR FBAPI FBFreeConstant(LPSTR cname);
  340.  FBERROR FBAPI FBFreeConstants();
  341.  
  342. /*==========================================================================
  343.  *
  344.  *  CALLBACK ROUTINES
  345.  *
  346.  *  Register functions to handle external variables. Setting callbacks overrides
  347.  *  the internal variable handling routines. All variables must be handled
  348.  *  externally
  349.  *
  350.  *==========================================================================*/
  351.  
  352.   FBERROR FBAPI  FBSetVariableCallbacks(HEXPR handle,
  353.                                         TCBKFindVariable CBKVFind,
  354.                                         TCBKGetVarValue  CBKVGetVal,
  355.                                         TCBKSetVarValue  CBKVSetVal,
  356.                                         LONG  lCBKData);
  357. //
  358. /*   FBERROR FBAPI  FBSetFieldCallbacks(HEXPR handle,
  359.                                         TCBKFindVariable CBKFFind,
  360.                                         TCBKGetVarValue  CBKFGetVal,
  361.                                         TCBKSetVarValue  CBKFSetVal,
  362.                                         LONG lCBKData);
  363. */
  364.  
  365. /*=======================================================================
  366.  *
  367.  *  FUNCTION MANAGEMENT ROUTINES
  368.  *
  369.  *
  370.  *========================================================================*/
  371.  
  372.   /*
  373.    *  Returns the number of functions registered with FB
  374.    */
  375.   WORD FBAPI FBGetFunctionCount(void);
  376.  
  377.   /*
  378.    * enumerate through all FormulaBuilder functions, calling fnCBK on
  379.    * each iteration. LParam is passed to the callback on each iteration
  380.    *
  381.    */
  382.  
  383.   FBERROR FBAPI FBEnumFunctions(TCBKEnumFunctions fnCbk,LONG lEnumdata);
  384.  
  385.   /*
  386.    * Register a function with FB
  387.    *
  388.    */
  389.   FBERROR FBAPI FBRegisterFunction(LPSTR       fname,
  390.                                    BYTE        returntype,
  391.                                    LPSTR       params,
  392.                                    int         minparms,
  393.                                    TCBKExternalFunc func);
  394.  
  395. /*=======================================================================
  396.  *
  397.  *  UTILITY ROUTINES
  398.  *
  399.  *======================================================================*/
  400.  
  401.  
  402.   /* Copy up to maxlen chars of an FB string to a null terminated string */
  403.  
  404.   void FBAPI  FBStrncpy(LPSTR dest,TFBString source,WORD maxlen);
  405.  
  406.  
  407.   /* Return a copy of the value structure */
  408.   TVALUEREC FBCopyValue(TVALUEREC value);
  409.  
  410.  
  411.   /* Convert a null terminated string to an FB date type */
  412.  
  413.   void FBAPI  FBlpzToDate(LPSTR source,LPFBDATE value);
  414.  
  415.   void FBAPI  FBDateToLpz(LPSTR dest,TFBDATE date,WORD maxlen);
  416.  
  417.   /* Convert an FB string to an FB date */
  418.  
  419.   void FBAPI  FBStringToDate(TFBString source,LPFBDATE value);
  420.  
  421.  /* Convert a null-terminated string to an FB string */
  422.   TBString  FBAPI  FBStringFromLPZ(LPSTR s);
  423.  
  424.  
  425. #endif  // __cplusplus
  426.  
  427. #endif // __FB_H_
  428.